package org.liang.agile.thread;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ThreadRunner implements Runnable {

    public static void main(String[] args) {
        final int MAXIMUM_CAPACITY = 1 << 30;
        System.out.println(MAXIMUM_CAPACITY);
    }

    int i;

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
            i = 10;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}



class Test {

    public static void main(String[] args) {
        try {
            nio();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    static void nio() throws IOException {
        RandomAccessFile input = new RandomAccessFile("D://a.txt", "rw"); // get the InputStream
                                                                          // from the client
        ByteBuffer buffer = ByteBuffer.allocate(48);
        FileChannel chinal = input.getChannel();
        int bytesRead = chinal.read(buffer);
        while (bytesRead != -1) {
            // System.out.println(bytesRead);
            buffer.flip();
            while (buffer.hasRemaining()) {
                System.out.print((char) buffer.get());
            }

            buffer.clear();
            bytesRead = chinal.read(buffer);
        }
        input.close();

    }


    void testloop() {
        List<String> datas = new ArrayList<String>();
        datas.add("1");
        datas.add("2");
        datas.add("3");
        datas.add("4");
        datas.add("5");
        datas.add("6");
        datas.add("7");
        datas.add("8");
        int i = 0;
        loop(datas, i);
        System.out.println("最后:" + datas.get(1));
    }

    static void loop(List<String> datas, int index) {
        if (datas.size() < 3) return;
        System.out.println("开始报数");
        Iterator<String> it = datas.iterator();
        while (it.hasNext()) {
            String value = it.next();
            index++;
            if (index % 3 == 0) {
                it.remove();
                index = 0;
                System.out.println(value + "退出");
            } else {
                System.out.println(value + " " + index);
            }
        }
        loop(datas, index);
    }

    void test() {
        ThreadRunner a = new ThreadRunner();
        Thread t = new Thread(a);
        t.start();
        try {
            // a.wait();
            // t.wait();
            // t.join();
            // Thread.yield();
            // t.notify();
            // a.notify();
            // t.interrupt();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int j = a.i;
        System.out.println(j);

    }
}
